﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;

//https://go.microsoft.com/fwlink/?LinkId=234236 上介绍了“用户控件”项模板

namespace XiaoweiUWP.HpControls
{
    public sealed partial class Introduce : UserControl
    {
        public Introduce()
        {
            this.InitializeComponent();
            this.Loaded += Introduce_Loaded;
        }
        private void Introduce_Loaded(object sender, RoutedEventArgs e)
        {

            imageChangeStoryBoard.Begin();
        }
        public static DependencyProperty AnimationKeyValueProperty { get; } = DependencyProperty.Register(
       "AnimationKeyValue", typeof(double), typeof(Introduce), new PropertyMetadata(0.0, AnimationKeyValuePropertyChanged)
       );
        private static void AnimationKeyValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Introduce page = (Introduce)d;
            page.ResetImageState();
        }
        private void ResetImageState()
        {
            int index1 = ((int)AnimationKeyValue / 20) % 3;
            int index2 = (index1 + 1) % 3;
            double offset = AnimationKeyValue - (index1 * 20) - 16;
            if (offset > 4) offset = 4;
            if (offset < 0) offset = 0;
            for (int i = 0; i < 3; i++)
            {
                if (i != index1 && i != index2)
                {
                    ((Rectangle)processBarShowPanel.Children[i]).Width = 4.6;
                    ((Rectangle)processBarShowPanel.Children[i]).Opacity = 0.6;
                    imageShowPanel.Children[i].Opacity = 0;
                    textShowPanel.Children[i].Opacity = 0;
                }
                else if (i == index1)
                {
                    ((Rectangle)processBarShowPanel.Children[i]).Width = (int)(15 + (5 - 15) * offset / 4);
                    ((Rectangle)processBarShowPanel.Children[i]).Opacity = 1 + (0.6 - 1) * offset / 4;
                    imageShowPanel.Children[i].Opacity = 1 + (0 - 1) * offset / 4;
                    ((FrameworkElement)imageShowPanel.Children[i]).Margin = new Thickness(
                        0 + (-264 - 0) * offset / 4,
                        0,
                        0 + (264 - 0) * offset / 4,
                        0
                        );
                    textShowPanel.Children[i].Opacity = 1 + (0 - 1) * offset / 4;
                }
                else if (i == index2)
                {
                    ((Rectangle)processBarShowPanel.Children[i]).Width = 20 - ((Rectangle)processBarShowPanel.Children[index1]).Width;
                    ((Rectangle)processBarShowPanel.Children[i]).Opacity = 0.6 + (1 - 0.6) * offset / 4;
                    imageShowPanel.Children[i].Opacity = 0 + (1 - 0) * offset / 4;
                    ((FrameworkElement)imageShowPanel.Children[i]).Margin = new Thickness(
                        264 + (0 - 264) * offset / 4,
                        0,
                        -264 + (0 + 264) * offset / 4,
                        0
                        );
                    textShowPanel.Children[i].Opacity = 0 + (1 - 0) * offset / 4;
                }
            }
        }
        public double AnimationKeyValue
        {
            get
            {
                return (double)GetValue(AnimationKeyValueProperty);
            }
            set
            {
                SetValue(AnimationKeyValueProperty, value);
            }
        }
    }
}
